<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Redis;


class HmyUser extends Admin
{

    // 查询数据
    public function index(Request $request)
    {

        // 查询数控
        $res = $request->get();

        if (!empty($res['title'])) {

            $data = $res['title'];

            $where['name'] = ['like', '%'.$data.'%'];

            $count = Db::table('hmy_user')->where('status',1)->where($where)->paginate(5)->count();
            // var_dump($count);

            if ($count == 0) {

                 $this->success('未找到查询的内容');
            }

        }else{

            $where = "";

        }

        // 查询数据
        $data = Db::table('hmy_user')->field(true)->where('status',1)->where($where)->paginate(5);

        //查询首分类
        
        //获取分页显示
        $page = $data->render();

        // 渲染模板
        $this->assign('page', $page);
        $this->assign('data', $data);

        // 返回
        return $this->fetch('hmy_user/index');

    }


    // 查询数据
    public function disable(Request $request)
    {

        // 查询数控
        $res = $request->get();

        if (!empty($res['title'])) {

            $data = $res['title'];

            $where['name'] = ['like', '%'.$data.'%'];

            $count = Db::table('hmy_user')->where('status',2)->where($where)->paginate(5)->count();

            if ($count == 0) {

                 $this->success('未找到查询的内容');
            }

        }else{

            $where = "";

        }

        // 查询数据
        $data = Db::table('hmy_user')->field(true)->where('status',2)->where($where)->paginate(5);

        //查询首分类
        
        //获取分页显示
        $page = $data->render();

        // 渲染模板
        $this->assign('page', $page);
        $this->assign('data', $data);

        // 返回
        return $this->fetch('hmy_user/disable');

    }


    // 查询语句
    public function find($id)
    {

        // 查询
        $data = Db::table('hmy_user')->field(['pass'],true)->find($id);

        // 跳转
        return view('admin@hmy_user/find', ['data' => $data]);

    }


    public function status(Request $request)
    {

        $id = $request->get('id');

        $data = Db::table('hmy_user')->where('id',$id)->find();


        // 改变值
        $data['status'] = $data['status'] == 2?'1':'2';

        $res = $data['status'] == 1?'已启用':'已禁用';

        // 插入
        $sql = DB::table('hmy_user')->where('id',$id)->update($data);


        // 返回结果
        if ($sql > 0) {

            
            $info['status'] = true;
            $info['msg'] = $res;

            return json($info);

        } else {

                    
            $info['status'] = false;
            $info['msg'] = '操作失败';

            return json($info);


        }
    }


}


